package com.bz.xtcx.manager.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.bz.xtcx.manager.entity.SysMenu;
import com.bz.xtcx.manager.entity.SysRole;
import com.bz.xtcx.manager.enums.StatusEnum;
import com.bz.xtcx.manager.mapper.SysMenuMapper;
import com.bz.xtcx.manager.service.ISysMenuService;
import com.bz.xtcx.manager.vo.VoQuery;
import com.bz.xtcx.manager.vo.VoResponse;
import com.github.pagehelper.PageInfo;

@Service
public class SysMenuService extends BaseService implements ISysMenuService{

	@Autowired
	private SysMenuMapper sysMenuMapper;
	
	@Override
	public VoResponse saveOrUpdate(SysMenu menu) {
		VoResponse voRes = new VoResponse();
		if(StringUtils.isEmpty(menu.getIsMenu()) || "1".equals(menu.getIsMenu())) {
			menu.setIsMenu("1"); //默认是页面
		}else {
			menu.setIsMenu("0");
		}
		if(StringUtils.isEmpty(menu.getId())) {//add
			menu.setStatus(StatusEnum.Enable.value());
			if(StringUtils.isEmpty(menu.getParentId())) {
				menu.setMenuLevel(1);
			}else {
				SysMenu permenu = sysMenuMapper.findById(menu.getParentId());
				if(permenu == null) {
					voRes.setFail(voRes);
					return voRes;
				}
				menu.setMenuLevel(permenu.getMenuLevel() + 1);
			}
			sysMenuMapper.insert(menu);
		}else {//update
			//SysMenu old = sysMenuMapper.findById(menu.getId());
			sysMenuMapper.update(menu);
		}
		return voRes;
	}

	@Override
	public int del(String id) {
		int result = sysMenuMapper.del(id);
		return result;
	}

	@Override
	public List<SysMenu> getAll() {
		List<SysMenu> list = sysMenuMapper.findTree();
		List<SysMenu> myMenus = new LinkedList<SysMenu>();
		if(list != null && list.size() > 0) {
			for(SysMenu menu : list) {
				if(StringUtils.isEmpty(menu.getParentId())) {//父目录顺序
					if(menu.getIsMenu() != null) {
						menu.setMenuLevel(1);
						setTreeSub(menu, list);
						myMenus.add(menu);
					}
				}
			}
		}
		this.setMenu(myMenus, null);
		return myMenus;
	}

	private void setTreeSub(SysMenu par, List<SysMenu> listAll) {

		for(SysMenu menu : listAll) {
			if(StringUtils.isEmpty(menu.getParentId())) {//父目录
				continue;

			} else if(menu.getParentId().equals(par.getId())) {//子目录
				if(par.getMenus() == null) {
					par.setMenus(new LinkedList<SysMenu>());
				}
				menu.setMenuLevel(par.getMenuLevel() + 1);
				par.getMenus().add(menu);
				setTreeSub(menu, listAll);
			}
		}
	}

    private List<SysMenu> setMenu(List<SysMenu> list, SysMenu lastMenu){
        Iterator<SysMenu> it = list.iterator();
        List<SysMenu> listnull = new ArrayList<>();
        while (it.hasNext()) {
            SysMenu menu = it.next();
            menu.setLeaf(true);
            if(menu.getMenus() != null && menu.getMenus().size() > 0 && menu.getMenuLevel() < 1000){
                boolean isLeafMenu = true;
                for (SysMenu subMenu : menu.getMenus()) {
                    if ("1".equals(subMenu.getIsMenu())) {
                        isLeafMenu = false;
                        break;
                    }
                }
                menu.setLeaf(isLeafMenu);
                setMenu(menu.getMenus(), menu);
            }else if(menu.getMenuLevel() == 1000){
                lastMenu.setAuthmenus(list);
                lastMenu.setMenus(listnull);
                lastMenu.setLeaf(true);
            }
        }
        return list;
    }

	@Override
	public PageInfo<SysMenu> getPageByCondition(VoQuery t, int pageNum, int pageSize, String orderBy) {
		// TODO Auto-generated method stub
		return null;
	}

}
